<template>
  <!-- 弹出框 -->
  <el-dialog
    :title="showTitle"
    :visible="showDialog"
    @close="handleClose"
    @open="handleOpen"
  >
    <!--
          el-form: model rules
          el-form-item: prop
          el-input:v-model
     -->
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <!-- el-option: label给用户看 value提交后端 -->
          <el-option v-for="item in employeeList" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="clickReset">取消</el-button>
      <el-button type="primary" @click="clickSubmit">确定</el-button>
    </template>
  </el-dialog>

</template>

<script>
import { reqGetEmployeeSimple } from '@/api/employee'
import { reqAddDepartment, reqGetDepartDetail, reqUpdateDepartments } from '@/api/department'
export default {
  name: '',
  components: {},
  props: {
    showDialog: {
      type: Boolean,
      defalut: false
    },
    nodeData: {
      type: Object,
      required: true
    },
    listData: {
      type: Array,
      required: true
    }
  },
  data() {
    const validateName = (rules, value, callback) => {
    //   可以得到nodeData(财务部) => 三个儿子
    //   console.log(value)
    //   console.log(this.nodeData) 财务部 448
    //   console.log(this.listData) 所有的数据

      // 新增:nodeData:父
      // 修改:nodeData:自己
      let arr = []
      if (this.form.id) {
        if (value === this.nodeData.name) {
          callback()
          return
        }
        arr = this.listData.filter(item => this.nodeData.pid === item.pid)
      } else {
        // 增校验
        arr = this.listData.filter(item => item.pid === this.nodeData.id)
      }

      const isRepeat = arr.some(item => item.name === value)
      isRepeat ? callback(new Error('部门名称已存在')) : callback()
    }
    // 自定义部门编码
    const validateCode = (rules, value, callback) => {
      // 在编辑状态下不修改code,也是放行   10012===10011
      if (this.form.id && value === this.nodeData.code) {
        callback()
        return
      }

      const isRepeat = this.listData.some((item) => item.code === value)
      isRepeat ? callback(new Error('部门编码已存在')) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称1~50位之间', trigger: ['blur', 'change'] },
          { validator: validateName, trigger: ['blur'] }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码1~50位之间', trigger: ['blur', 'change'] },
          { validator: validateCode, trigger: ['blur'] }
        ],
        manager: [
          { required: true, message: '请输入部门负责人', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍1~300位之间', trigger: ['blur', 'change'] }
        ]
      },
      employeeList: []
      // showTitle: '标题'
    }
  },
  computed: {
    showTitle() {
      // 新增 form没id 编辑中有id
      return this.form.id ? '编辑部门' : '新增子部门'
    }
  },
  watch: {},
  created() {
    // console.log('获取员工信息')
  },
  mounted() {},
  methods: {
    handleClose() {
    //   console.log('点击了')
    // 父组件向子组件传值,子组件不能修改,单向数据流
    // this.showDialog = false
    // this.$emit('close-dept')
      this.$emit('update:showDialog', false)
      this.$refs.deptForm.resetFields()
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
    },
    async handleOpen() {
      const { data } = await reqGetEmployeeSimple()
      this.employeeList = data
    },
    async clickSubmit() {
      this.$refs.deptForm.validate(async flag => {
        if (!flag) return

        if (this.form.id) {
          // 编辑
          await reqUpdateDepartments(this.form)
          // 提示信息
          this.$message.success('修改部门成功')
        } else {
          // 新增
          await reqAddDepartment({
            ...this.form,
            pid: this.nodeData.id
          })

          // 提示信息
          this.$message.success('添加部门成功')
        }

        // 关闭弹层
        this.handleClose()

        // 触发重新获取数据事件
        this.$emit('add-detps')
      })
    },
    clickReset() {
      // 1.关闭
      this.handleClose()
    },

    // 根据部门id获取部门的详细信息   可能只有nodeData(id,name,manager)
    async getDepartmentDetail() {
      // 获取部门详情
      const { data } = await reqGetDepartDetail(this.nodeData.id)
      this.form = data
    }
  }
}
</script>

<style scoped lang='scss'>

</style>
